<?php
namespace app\admin\controller;
use think\Config;
use think\Session;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Vfpclassify extends Base
{
    /**
     * 数据展示
     * @return html
     */
    public function index(){
        $data = input();
        $where = array();
        $field = "id,grade_id,name,pic,sort,is_show";
        // 查询状态为1的用户数据 并且每页显示5条数据
        $list = db("eb_special_subject")->where($where)->field($field)->paginate( 5, false, ['query'=>request()->param()] );
        $data  = $list->items();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $cate = db("eb_special_subject")->select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    
    /**
     * 添加页面
     * @return html
     */
    public function add(){
        return $this->fetch();
    }

    public function toadd(){
        if(request()->isPost()){
            $data = input();
            $Vfpclassify = model('Vfpclassify');
            $res = $Vfpclassify->toadd($data);
            if($res>0){
                $this->success('添加成功',url('Vfpclassify/index'));
            }else{
                $this->success('添加失败',url('Vfpclassify/index'));
            }
        }else{
            $cate = db("eb_special_subject")->select();
            $this->assign('cate',$cate);
            return $this->fetch();
        }
    }

    /**
     * 修改分类
     * @return html
     */
    public function edit(){
        $id  = input( 'id' );
        if(request()->isPost()){
            $inputdata = input();   
            $data = array();
            $data['grade_id'] = $inputdata['grade_id'];
            $data['name'] = $inputdata['name'];
            $data['pic'] = $inputdata['pic  '];
            $data['sort'] = $inputdata['sort'];
            $data['is_show'] = $inputdata['is_show'];
            $Classify = model('Vfpclassify');
            $res = $Classify->editMember( $inputdata['id'],$data );
            if( $res ) {
                $this->success( '编辑成功', url( 'Vfpclassify/index' ) );
            } else {
                $this->error( '编辑失败' , url( 'Vfpclassify/edit' ));
            }
        }else{
            $find  = db( 'eb_special_subject' )->where( ['id'=>$id] )->find();
            $this->assign( 'find', $find );
            $this->assign( 'id', $id );
            return $this->fetch();
        }
    }

    /**
     * 删除分类
     * @return html
     */
    public function delete(){
        $id = input('id');
        if(db('eb_special_subject')->where('id',$id)->delete()){
            $this->success('删除成功' , url("Vfpclassify/index"));
        } else {
            $this->error('删除失败');
        }
    }

    /**
    * 上传图片
    * @return html
    */
    public function upload()
    {
        $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = "wsMDdHxSgkZ-J22N9AkLro1qV4ip6yfhqZr7c8LC";
        $secretKey = "NIeB_SX89SZLacalECbRlW0N-ozOpq8ZxZ2XzR7m";
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = "hy1810a";

        //域名
        $domain = "quu1cz5a6.hb-bkt.clouddn.com";
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }

    }
}

?>

